package com.utils
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import mx.core.INavigatorContent;
	
	import org.osmf.metadata.IFacet;
	
	import spark.components.mediaClasses.VolumeBar;

	public class Utils
	{
		public function Utils()
		{
		}
		/**
		 *转换byteArray成16进制仅仅用于显示而已 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function analysisto16(data:ByteArray):String
		{
			var dataStr:String="";
			var index:int=0;
			if(data.length>0&&data.bytesAvailable>0){
				while(index<data.length){
					dataStr+=(data[index].toString(16)+" ");
					index++;
				}
				
				return dataStr;
			}
			return "";
		}
		/**
		 *读取头字节 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readHead(data:ByteArray):int
		{
			data.endian = Endian.LITTLE_ENDIAN;
			var code:uint = data.readUnsignedInt();
			data.position = (code + 4);
			
			return data.readUnsignedInt();	
		}
		/**
		 *写入头字节 
		 * @param code
		 * @return 
		 * 
		 */		
		public static function writeHead(code:uint):ByteArray
		{
			var souceByte:ByteArray = new ByteArray();
			souceByte.endian = Endian.LITTLE_ENDIAN;
			souceByte.writeUnsignedInt(code);
			souceByte.position= code+4;
			return souceByte;
		}
		/**
		 *英雄王座读取字符串方法 
		 * @param _arg1
		 * @return 
		 * 
		 */		
		public static function readString(_arg1:ByteArray):String
		{
			var _local2:* = 0;
			var _local3:* = _arg1.position;
			while (_local3 < _arg1.length)
			{
				if (_arg1[_local3] == 0)
				{
					_local2 = ((_local3 - _arg1.position) + 1);
					break;
				}
				_local3++;
			}
			if (_local2 > 0)
			{
				return (_arg1.readMultiByte(_local2, "cn-gb"));
			}
			return "";
		}
		
		public static function writeString(str:String,sourceByteArray:ByteArray):void
		{
			var index:int=0;
			
			var len:int = str.length;
			while(index<len){
				sourceByteArray.writeMultiByte(str.charAt(index),"cn-gb");
				if(index==len-1){
					sourceByteArray.writeByte(0);
					break;
				}
				
				index++;
			}
		}
		
		public static function writeList(strList:String,sourceByteArray:ByteArray):void
		{
			var chartList:Array = strList.split(",");
			var chartNum:int = chartList.length;
			var index:int=0;
			while(index<chartNum){
				sourceByteArray.writeUnsignedInt(chartList[index]);
				index++;
			}
		}
		
		public static function writeList2(strList:String,sourceByteArray:ByteArray):void
		{
			var chartList:Array = strList.split("-");
			var chartNum:int = chartList.length;
			var index:int=0;
			while(index<chartNum)
			{
				sourceByteArray.writeUnsignedInt(chartList[index]);
				index++;
			}
		}
		
		public static function writeList3(strList:String,sourceByteArray:ByteArray):void
		{
			var chartList:Array = strList.split("-");
			var chartNum:int = chartList.length;
			var index:int=0;
			
			sourceByteArray.writeUnsignedInt(chartList[0]);
			sourceByteArray.writeUnsignedInt(chartList[1]);
			sourceByteArray.writeUnsignedInt(chartList[2]);
			
			writeString(chartList[3],sourceByteArray);
			
		}
		
		

		
		public static function xmlToArray($str:String,sourceByteArray:ByteArray,flag:Boolean = false):void
		{
			var arrs:Array = ToArray($str);
			
			if(flag)
			{
				for(var j:int=0;j<arrs.length;j++)
				{
					writeList3(arrs[j],sourceByteArray);
				}
			}
			else
			{
				for(var i:int=0;i<arrs.length;i++)
				{
					writeList2(arrs[i],sourceByteArray);
				}
			}
			
			
		}
		
		
		
		/**
		 * 传入一个数String得到一个不大于三维的Array
		 * @param        $str
		 * @return  retArr
		 */
		public static function ToArray($str:String):Array 
		{
			//去掉换行字符
			$str = $str.split("\n").join("");
			//去掉回车字符
			$str = $str.split("\r").join("");
			//返回的数组
			var retArr:Array = [];
			
			if($str.slice(0,3) == "[[[")
			{
				//trace("三维字符串");
				$str = $str.split("[[[").join("");
				$str = $str.split("]]]").join("");
				retArr = arr3($str.split("]],[["));
				
			}
			else if($str.slice(0,2) == "[[")
			{
				//trace("二维字符串");
				$str = $str.split("[[").join("");
				$str = $str.split("]]").join("");
				retArr = arr2($str.split("],["));
				
			}
			else
			{
				//trace("一维字符串");
				$str = $str.slice(1,-1);
				retArr.splice("-");
			}
			
			return retArr;
		}
		
		private static function arr3($arr:Array):Array
		{
			var anArr:Array = [];
			//最外维
			for(var i:uint=0;i<$arr.length;i++)
			{
				anArr[i] = [];
				//第二维
				anArr[i] = $arr[i].split("],[");
				for(var j:uint=0;j<anArr[i].length;j++)
				{
					//第三维
					anArr[i][j] = anArr[i][j].split(",");
				}
			}
			
			return anArr;
		}
		
		private static function arr2($arr:Array):Array
		{
			for(var i:uint=0;i<$arr.length;i++)
			{
				$arr[i].split(",");
			}
			return $arr;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}